<template>
	<view class="shzy-wrap shzy-wrap-form">

		<view class="search-box search-box_sticky" style="height: 240rpx" v-show="this.type == 'ration'">
			<u-form-item label="检验值" prop="comInput" label-width="150">
				<u-input v-model="model.StandardValue + model.Unit" placeholder="" disabled="disabled">

				</u-input>
			</u-form-item>
			<!-- 单行输入 -->
			<u-form-item label="检验标准" prop="comInput" label-width="150">
				<u-input v-model="type == 'qualitative' ? model.Remarks : model.SpStandardValue" placeholder=""
					disabled="disabled">

				</u-input>
			</u-form-item>
		</view>

		<u-form :rules="rules" :errorType="['toast']" label-position="left" label-width="150" label-align="left"
			class="shzy-form">


			<u-form-item>
				<u-icon v-show="model.Destructiveness =='Y' " name="star-fill" color="#ff0000" size="28"
					style="margin-right: 5px;"></u-icon>
				<u-input v-model="text" placeholder="" style="flex:0.3;" disabled="disabled"></u-input>
				<u-input v-model="model.DestructivenessQty" placeholder="请输入" type="number" style="flex:0.7;"></u-input>
			</u-form-item>

			<!-- 显示文件 -->
			<u-form-item label="附件" prop="uploadImg">
				<!-- <shzy-upload v-model="dataForm.uploadImg"></shzy-upload> -->
				<shzy-file v-model="model.FileInfo" showButton disabled/>

				<!-- 		<text @click="downloadFile" >{{model.FileInfo.length > 0 ? model.FileInfo[0].name : '暂无附件' }}</text> -->
			</u-form-item>


			<view class="shzy-card">
				<view style="background-color: white;" v-if="list.length > 0">
					<view v-for="(item, index) in list" :key="item.id" :threshold="0"><!-- :right-options="options" -->
						<view class="item-list" :id="'item'+index" ref="mydom" style="height: 50px;"
							v-if="index < model.SamplingQty">
							<view class="item-left">
								<u-form-item >

									<view style="width:100%; display: flex; align-items: center;">

										<view style="flex:0.3;">
											<u-input v-model="item.columnName" placeholder=""></u-input>
										</view>
										<view style="flex:0.6;">
											<!-- 定性 -->
											<view v-if="type == 'qualitative'" style="float: right;">

												<u-tag v-if="model['column'+(index+1)].defectQty > 0" text='填写缺陷'
													type='error' mode="light" @click='qualitativeClick(index)' />
												<u-tag v-else text='填写缺陷'
													type='primary' mode="light" @click='qualitativeClick(index)' />

												<!-- <u-tag text='填写缺陷' type='primary' mode="light"
													@click='qualitativeClick(index)' style="float: right;"/> -->

											</view>

											<!-- 定量 -->
											<view v-else>
												<u-input v-model="model['column'+(index+1)].value"
													@input="inputChange($event,index)" placeholder="请输入"
													type="number"></u-input>

											</view>
										</view>
										<view style="flex:0.1;">
											<!-- v-show="iconShow(model['column'+(index+1)].value)" -->
											<u-icon :name="iconName(model['column'+(index+1)].value)"
												:color = "iconName(model['column'+(index+1)].value) == 'close' ? '#ff0000' : '#00ff00'" size="30"></u-icon>
										</view>
									</view>
								</u-form-item>
							</view>
						</view>
					</view>
				</view>

			</view>

		</u-form>
		<view class="buttom-actions">

			<u-button class="buttom-btn" type="primary" @click="submit">保存</u-button>

		</view>
	</view>
</template>

<script>
	import {
		getInspectionitemdefectList,
	} from '@/api/q1/business/inspection/ipqcinspectionlot.js'
	import define from '@/utils/define.js'
	import {
		inspectionitem
	} from '@/api/q1/business/inspection/ipqcinspectionlot.js'
	export default {
		data() {
			return {
				text: '破坏性数量',
				list: {},
				model: {
					SpStandardValue: "",
					sn: "",
					batch: ""
				},
				dataForm: {},
				rules: {},
				type: '',
				formData: {},
				column:0
			};
		},
		//this.model.Remarks
		onLoad(option) {

			const json = JSON.parse(option.params)
			
			this.type = json.type;
			this.formData = json.formData;
			this.model = json.model;
			this.list = json.customColumnList;

			if (this.model.QuantitativeStandards == "1") {
				this.model.SpStandardValue = this.model.MinimumValue + this.model.Unit + " ~ " + this.model.MaximumValue +
					this.model.Unit
			} else if (this.model.QuantitativeStandards == "2") {
				this.model.SpStandardValue = '<' + this.model.StandardValue + this.model.Unit
			} else if (this.model.QuantitativeStandards == "3") {
				this.model.SpStandardValue = '>' + this.model.StandardValue + this.model.Unit
			} else if (this.model.QuantitativeStandards == "4") {
				this.model.SpStandardValue = '=' + this.model.StandardValue + this.model.Unit
			} else if (this.model.QuantitativeStandards == "5") {
				this.model.SpStandardValue = '<=' + this.model.StandardValue + this.model.Unit
			} else if (this.model.QuantitativeStandards == "6") {
				this.model.SpStandardValue = '>=' + this.model.StandardValue + this.model.Unit
			} else if (this.model.QuantitativeStandards == "7") {
				this.model.SpStandardValue = (this.model.StandardValue - this.model.LowerDeviation) + this.model.Unit +
					"~" + (this.model.StandardValue + this.model.UpperDeviation) + this.model.Unit
			}
			//const arr = JSON.parse((this.model.Parameter))
			// if(arr.length >0){
			// this.$set(this.model, 'sn', arr[0].value ? arr[0].value :'')
			// this.$set(this.model, 'batch', arr[1].value ? arr[1].value :'')	
			// }
		},
		onShow(){	// 页面显示就会执行
			
			// 将这个index传到定性页面,定性页面保存将数量的值++,传到当前页面赋值给this.model[column].defectQty即可
			if(this.column != 0) {
				this.getInspectionitemdefectListApi();
			}
		},
		computed: {
			iconShow() {
				return (data) => {

					if (this.model.QuantitativeStandards == "1") {
						if (this.model.MinimumValue <= data && data <= this.model.MaximumValue) {
							return 'OK';
						} else {
							return 'NG';
						}
					} else if (this.model.QuantitativeStandards == "2") {
						if (data < this.model.StandardValue) {

							return 'OK';
						} else {
							return 'NG';
						}
					} else if (this.model.QuantitativeStandards == "3") {
						if (data > this.model.StandardValue) {

							return 'OK';
						} else {
							return 'NG';
						}
					} else if (this.model.QuantitativeStandards == "4") {
						if (data == this.model.StandardValue) {

							return 'OK';
						} else {
							return 'NG';
						}
					} else if (this.model.QuantitativeStandards == "5") {
						if (data <= this.model.StandardValue) {

							return 'OK';
						} else {
							return 'NG';
						}
					} else if (this.model.QuantitativeStandards == "6") {
						if (data >= this.model.StandardValue) {

							return 'OK';
						} else {
							return 'NG';
						}
					} else if (this.model.QuantitativeStandards == "7") {
						if ((this.model.StandardValue - this.model.LowerDeviation) <= data && data <= (this.model
								.StandardValue + this.model.LowerDeviation)) {
							if (data > this.model.StandardValue) {

								return 'UP';
							} else if (data < this.model.StandardValue) {
								return 'Down';
							} else {
								return 'OK';
							}
						} else {
							return 'NG';
						}
					}



				}
			},
			iconName() {

				return (data) => {

					if (data != '') {
						if (this.model.QuantitativeStandards == "1") {
							if (this.model.MinimumValue <= data && data <= this.model.MaximumValue) {
								return 'checkmark';
							} else {
								return 'close';
							}

						} else if (this.model.QuantitativeStandards == "2") {
							if (data <= this.model.StandardValue) {
								return 'checkmark';
							} else {
								return 'close';
							}
						} else if (this.model.QuantitativeStandards == "3") {
							if (data > this.model.StandardValue) {
								return 'checkmark';
							} else {
								return 'close';
							}
						} else if (this.model.QuantitativeStandards == "4") {
							if (data == this.model.StandardValue) {
								return 'checkmark';
							} else {
								return 'close';
							}
						} else if (this.model.QuantitativeStandards == "5") {
							if (data <= this.model.StandardValue) {
								return 'checkmark';
							} else {
								return 'close';
							}
						} else if (this.model.QuantitativeStandards == "6") {
							if (data >= this.model.StandardValue) {
								return 'checkmark';
							} else {
								return 'close';
							}
						} else if (this.model.QuantitativeStandards == "7") {
							if ((this.model.StandardValue - this.model.LowerDeviation) <= data && data <= (this.model
									.StandardValue + this.model.LowerDeviation)) {
								if (data > this.model.StandardValue) {
									return 'arrow-upward';
								} else if (data < this.model.StandardValue) {
									return 'arrow-downward';
								} else {
									return 'checkmark';
								}
							} else {
								return 'close';
							}


						}
					}


				}
			}
		},

		methods: {
			// 调缺陷接口判断数量改变[填写缺陷]按钮颜色
			getInspectionitemdefectListApi() {
				let query = {
					inspectionItem: this.model.Id,
					number: this.column
				}
				getInspectionitemdefectList(query).then(res => {
					this.model['column' + this.column].defectQty = res.data.list.length;	// 定性model选中的column
				})
			},
			qualitativeClick(i) {
				this.column = i + 1;
				uni.navigateTo({
					url: '/pages/q1/business/processInspectionlot/qualitative?formData=' + encodeURIComponent(JSON
						.stringify(this.formData)) + '&index=' + i
				})

			},

			inputChange(e, i) {
				if (this.iconShow(e) == 'OK') {
					this.$set(this.model['column' + (i + 1)], 'status', 'OK')
				} else {
					this.$set(this.model['column' + (i + 1)], 'status', 'NG')
				}
			},

			//提交
			submit() {

				let arry = [];
				arry.push(this.model);
				let query = {
					code: this.model.Code,
					dataArray: arry,
					columnArray: this.list
				}

				inspectionitem(query).then(res => {
					uni.navigateBack({
						delta: 1
					});
				})
			},
		}
	};
</script>